package com.ayz.gateway.core.datasource.unpooled;



import com.ayz.gateway.core.context.config.Configuration;
import com.ayz.gateway.core.datasource.Connection;
import com.ayz.gateway.core.datasource.Datasource;
import com.ayz.gateway.core.datasource.DatasourceType;
import com.ayz.gateway.core.datasource.connection.DubboConnection;
import com.ayz.gateway.core.datasource.exception.DatasourceNotSupportException;
import com.ayz.gateway.core.rpc.config.DubboConfiguration;
import lombok.Data;

/**
 * @ClassName UnpooledDatasource
 * @Description TODO: 非池化的数据源 注意这里的非池化不意味着真正的非池化,因为第三方RPC框架的客户端可能有连接池
 *                                                                比如Dubbo,grpc等框架
 * @Author 快乐的星球
 * @Date 2023/5/9 11:37
 * @Version 1.0
 **/
@Data
public class UnpooledDatasource implements Datasource {
    /**
     * 数据源的类型
     */
    private DatasourceType datasourceType;
    /**
     * 网关上下文配置项
     */
    private Configuration configuration;
    public UnpooledDatasource(DatasourceType datasourceType, Configuration configuration) {
        this.datasourceType = datasourceType;
        this.configuration=configuration;
    }
    @Override
    public Connection getConnection() {
        switch (datasourceType){
            case HTTP:
                //这里先不开发HTTP,慢慢迭代式开发,后面可以引入SPI机制
                return null;
            case DUBBO:
                DubboConfiguration dubboConfiguration=(DubboConfiguration) configuration;
                return new DubboConnection(dubboConfiguration);
            default:
                break;
        }
        throw new DatasourceNotSupportException("目前不支持此连接数据源:{"+datasourceType.getName()+"}!");
    }
}
